package com.feng.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feng.commonutils.R;
import com.feng.servicebase.excecption.GuliException;
import com.feng.staservice.client.UcenterClient;
import com.feng.staservice.entity.StatisticsDaily;
import com.feng.staservice.mapper.StatisticsDailyMapper;
import com.feng.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author fengge
 * @since 2023-03-31
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public R registerCount(String day) {
        if (StringUtils.isEmpty(day)) {
            throw new GuliException(20001, "请选择日期");
        }
        //远程调用得到某一天注册人数
        Integer integer = ucenterClient.countRegister(day);
        //删除统计数据，在保存最新 数据
        LambdaQueryWrapper<StatisticsDaily> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StatisticsDaily::getDateCalculated, day);
        this.remove(queryWrapper);
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(integer);//注册人数
        statisticsDaily.setDateCalculated(day);//统计日期
        //TODO 课程数量
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100, 200));
        //TODO 登录数量
        statisticsDaily.setLoginNum(RandomUtils.nextInt(200, 300));//登录数
        //TODO 视频浏览数
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(200, 300));//视频流量数


        this.save(statisticsDaily);
        return R.ok();
    }

    @Override
    public R showData(String type, String begin, String end) {
        //根据条件查询对应的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.select("date_calculated", type);
        wrapper.between("date_calculated", begin, end);
        List<StatisticsDaily> list = this.list(wrapper);
        //前端要求数组json结果，对应后端为List集合
        //创建两个list集合，一个放日期X轴，一个放数量Y轴
        List<String> dataList = new ArrayList<>();
        List<Integer> numList = new ArrayList<>();
        dataList=  list.stream().map(e->{
            return e.getDateCalculated();
        }).collect(Collectors.toList());
        numList=list.stream().map(e-> {
                    //判断查询的哪个字段
                    if ("register_num".equals(type)) {
                        return e.getRegisterNum();
                    }
                    if ("login_num".equals(type)) {
                        return e.getLoginNum();
                    }
                    if ("video_view_num".equals(type)) {
                        return e.getVideoViewNum();
                    }
                    if ("course_num".equals(type)) {
                        return e.getCourseNum();
                    }
                    return null;

                }
        ).collect(Collectors.toList());
        Map<String,Object> map=new HashMap<>();
        map.put("data_calculatedList",dataList);
        map.put("numDataList",numList);
        return R.ok().data(map);
    }
}
